import 'package:duqing/components/singer_card.dart';
import 'package:duqing/models/user_model.dart';
import 'package:duqing/services/user_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

class SingerPage extends StatefulWidget {
  const SingerPage({Key key}) : super(key: key);

  @override
  _SingerPageState createState() => _SingerPageState();
}

class _SingerPageState extends State<SingerPage>
    with AutomaticKeepAliveClientMixin {
  List<UserItem> _userList = UserList([]).list;
  int page = 1;
  int limit = 10;
  bool hasMore = true;
  bool err = false;
  String errorMessage;

  EasyRefreshController _easyRefreshController;

  @override
  void initState() {
    super.initState();
    _easyRefreshController = EasyRefreshController();
    // _getUsers();
  }

  Future _onLoad() async {
    _getUsers(push: true);
    _easyRefreshController.finishLoad(noMore: !hasMore);
  }

  Future _onRefresh() async {
    page = 1;
    _getUsers();
    _easyRefreshController.resetLoadState();
  }

  @override
  void dispose() {
    _easyRefreshController.dispose();
  }

  Future _getUsers({bool push = false}) async {
    try {
      Map<String, dynamic> result = await UserService.getUsers(
          page: page, limit: limit, type: 'DQ_SINGER');

      UserList userListModel = UserList.fromJson(result['data']);

      setState(() {
        hasMore = page * limit < result['total'];
        page++;
        if (push) {
          _userList.addAll(userListModel.list);
        } else {
          _userList = userListModel.list;
        }
      });
    } catch (e, s) {
      err = true;
      errorMessage = e.toString();
      print(s);
    }
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return EasyRefresh(
      firstRefresh: true,
      controller: _easyRefreshController,
      onRefresh: _onRefresh,
      onLoad: _onLoad,
      child: GridView.builder(
        padding: EdgeInsets.only(
          top: 9,
        ),
        itemCount: _userList.length,
        gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
          mainAxisExtent: MediaQuery.of(context).size.width / 1.6,
          maxCrossAxisExtent: MediaQuery.of(context).size.width / 2,
          mainAxisSpacing: 8,
          crossAxisSpacing: 1,
        ),
        itemBuilder: (BuildContext context, int index) {
          bool isEven = index.isEven;
          double pl = isEven ? 18 : 9;
          double pr = isEven ? 9 : 18;
          return Container(
            padding: EdgeInsets.only(top: 9, left: pl, right: pr, bottom: 9),
            color: Colors.white,
            child: SingerCard(
              coverPictureUrl: _userList[index].coverPictureUrl,
              nickName: _userList[index].nickname,
              musicCount: _userList[index].musicCount,
              musicPlayCount: _userList[index].musicPlayCount,
            ),
          );
        },
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
